<script setup lang="ts">
import { ref, defineProps, reactive, computed, watch } from 'vue'

/**
 * 组件属性定义
 * @property {string} msg - 显示的消息文本
 */
const props = defineProps<{ msg: string }>()

/**
 * 计数器状态
 * @type {import('vue').Ref<number>}
 */
const count = ref(0)

/**
 * 计数器历史记录
 * @type {Array<{value: number, timestamp: Date}>}
 */
const history = reactive<Array<{value: number, timestamp: Date}>>([]);

/**
 * 计算属性：是否有历史记录
 */
const hasHistory = computed(() => history.length > 0);

/**
 * 计算属性：最大计数值
 */
const maxCount = computed(() => {
  if (history.length === 0) return 0;
  return Math.max(...history.map(item => item.value));
});

/**
 * 监听计数器变化，记录历史
 */
watch(count, (newValue) => {
  history.push({
    value: newValue,
    timestamp: new Date()
  });
  // 只保留最近的5条记录
  if (history.length > 5) {
    history.shift();
  }
});

/**
 * 重置计数器到初始值
 */
function resetCount() {
  count.value = 0;
  // 添加重置记录
  history.push({
    value: 0,
    timestamp: new Date()
  });
}

/**
 * 清空历史记录
 */
function clearHistory() {
  history.length = 0;
}
</script>

<template>
  <h1>{{ msg }}</h1>

  <div class="card">
    <div class="counter-container">
      <button type="button" @click="count--" class="decrement-btn" :disabled="count <= 0">-</button>
      <span class="count-display">{{ count }}</span>
      <button type="button" @click="count++" class="increment-btn">+</button>
    </div>
    <div class="button-group">
      <button type="button" @click="resetCount" class="reset-btn">重置</button>
      <button type="button" @click="clearHistory" class="clear-btn" :disabled="!hasHistory">清空历史</button>
    </div>
    
    <div v-if="hasHistory" class="history-container">
      <h3>历史记录 <span class="max-badge">最大值: {{ maxCount }}</span></h3>
      <ul class="history-list">
        <li v-for="(item, index) in history" :key="index" class="history-item">
          <span class="history-value">{{ item.value }}</span>
          <span class="history-time">{{ new Date(item.timestamp).toLocaleTimeString() }}</span>
        </li>
      </ul>
    </div>
    
    <p>
      Edit
      <code>components/HelloWorld.vue</code> to test HMR
    </p>
  </div>

  <p>
    Check out
    <a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
      >create-vue</a
    >, the official Vue + Vite starter
  </p>
  <p>
    Learn more about IDE Support for Vue in the
    <a
      href="https://vuejs.org/guide/scaling-up/tooling.html#ide-support"
      target="_blank"
      >Vue Docs Scaling up Guide</a
    >.
  </p>
  <p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
</template>

<style scoped lang="scss">
.read-the-docs {
  color: $pink;
}

.card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background-color: #fff;
  max-width: 400px;
  margin: 0 auto;
  
  .counter-container {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 10px;
    
    .count-display {
      font-size: 24px;
      font-weight: bold;
      min-width: 40px;
      text-align: center;
    }
    
    .increment-btn, .decrement-btn {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 18px;
      font-weight: bold;
      transition: all 0.2s ease;
      
      &:disabled {
        opacity: 0.5;
        cursor: not-allowed;
      }
    }
    
    .increment-btn {
      background-color: $primary;
      color: white;
      border: none;
    }
    
    .decrement-btn {
      background-color: #f5f5f5;
      color: #333;
      border: 1px solid #ddd;
    }
  }
  
  .button-group {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
  }
  
  .history-container {
    width: 100%;
    margin-top: 10px;
    padding: 15px;
    background-color: #f9f9f9;
    border-radius: 6px;
    border: 1px solid #eee;
    
    h3 {
      font-size: 16px;
      margin-top: 0;
      margin-bottom: 10px;
      color: #333;
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .max-badge {
        font-size: 12px;
        background-color: $primary;
        color: white;
        padding: 2px 8px;
        border-radius: 12px;
      }
    }
    
    .history-list {
      list-style: none;
      padding: 0;
      margin: 0;
      
      .history-item {
        display: flex;
        justify-content: space-between;
        padding: 8px 0;
        border-bottom: 1px solid #eee;
        font-size: 14px;
        
        &:last-child {
          border-bottom: none;
        }
        
        .history-value {
          font-weight: bold;
        }
        
        .history-time {
          color: #666;
          font-size: 12px;
        }
      }
    }
  }
  
  button {
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover:not(:disabled) {
      transform: translateY(-2px);
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }
    
    &:active:not(:disabled) {
      transform: translateY(0);
    }
  }
  
  .reset-btn {
    background-color: $primary;
    color: white;
    border: none;
    padding: 8px 20px;
    font-weight: 500;
  }
  
  .clear-btn {
    background-color: #f5f5f5;
    color: #333;
    border: 1px solid #ddd;
    padding: 8px 20px;
    font-weight: 500;
    
    &:disabled {
      opacity: 0.5;
      cursor: not-allowed;
      background-color: #f0f0f0;
    }
  }
}
</style>
